package com.example.springboottest.example.date;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.time.Instant;

public class DateTest {
    public static void main(String[] args) {
//        date2Instant();

//        date2LocalDate();

        date2LocalDateTime();
    }

    /**
     * Date 和 Instant 互相转化
     */
    public static void date2Instant() {
        Instant instant = Instant.now();
        System.out.println(instant.toString());

        Date date = Date.from(instant);
        System.out.println(date.toString());

        instant = date.toInstant();
        System.out.println(instant.toString());

    }

    /**
     * Date 和 LocalDate 互相转化
     */
    public static void date2LocalDate() {
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate.toString());

        Date date = Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(date.toString());

        localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        System.out.println(localDate.toString());
    }

    /**
     * Date 和 LocalDateTime 互相转化
     */
    public static void date2LocalDateTime() {
        Date date = new Date();
        System.out.println(date.toString());

        LocalDateTime localDateTime = LocalDateTime.from(date.toInstant().atZone(ZoneId.systemDefault()));
        System.out.println(localDateTime.toString());

        date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(date.toString());
    }
}